﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Input;
using System.Windows;

namespace WebViewerTest
{
	public class InputDelegator
	{
		private IntPtr hWnd;

		public InputDelegator( IntPtr hWnd )
		{
			this.hWnd = hWnd;
		}

		public void SendMouseMove( int x, int y )
		{
			Win32.User32.SendMessage( hWnd, Win32.User32.WM_MOUSEMOVE, (IntPtr)(long)0x1, (IntPtr)(int)( ( y << 16 ) | ( x & 0xFFFF ) ) );
		}
/*
		private static INPUT ProcessEvent( MouseEventArgs e, IInputElement relativeTo, uint flags )
		{
			var p = e.GetPosition( relativeTo );

			return new INPUT
			{
				mi = new MOUSEINPUT
				{
					dwExtraInfo = IntPtr.Zero,
					dx = (int)p.X,
					dy = (int)p.Y,
					time = 0,
					mouseData = 0,
					dwFlags = flags
				}
			};
		}

		private void Send( INPUT[] inputs )
		{
			var focus = GetFocus();
			SetFocus( hWnd );

			try
			{
				SendInput( (uint)inputs.Length, inputs, Marshal.SizeOf( typeof( INPUT ) ) );
			}
			finally
			{
				SetFocus( focus );
			}
		}
*/
/*
		const int INPUT_MOUSE = 0;
		const int INPUT_KEYBOARD = 1;
		const int INPUT_HARDWARE = 2;
		const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
		const uint KEYEVENTF_KEYUP = 0x0002;
		const uint KEYEVENTF_UNICODE = 0x0004;
		const uint KEYEVENTF_SCANCODE = 0x0008;
		const uint XBUTTON1 = 0x0001;
		const uint XBUTTON2 = 0x0002;
		const uint MOUSEEVENTF_MOVE = 0x0001;
		const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
		const uint MOUSEEVENTF_LEFTUP = 0x0004;
		const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
		const uint MOUSEEVENTF_RIGHTUP = 0x0010;
		const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
		const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
		const uint MOUSEEVENTF_XDOWN = 0x0080;
		const uint MOUSEEVENTF_XUP = 0x0100;
		const uint MOUSEEVENTF_WHEEL = 0x0800;
		const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
		const uint MOUSEEVENTF_ABSOLUTE = 0x8000;

		[DllImport( "user32.dll", SetLastError = true )]
		static extern uint SendInput( uint nInputs, INPUT[] pInputs, int cbSize );

		[StructLayout( LayoutKind.Sequential )]
		struct MOUSEINPUT
		{
			public int dx;
			public int dy;
			public uint mouseData;
			public uint dwFlags;
			public uint time;
			public IntPtr dwExtraInfo;
		}

		[StructLayout( LayoutKind.Sequential )]
		struct KEYBDINPUT
		{
			public ushort wVk;
			public ushort wScan;
			public uint dwFlags;
			public uint time;
			public IntPtr dwExtraInfo;
		}

		[StructLayout( LayoutKind.Sequential )]
		struct HARDWAREINPUT
		{
			public uint uMsg;
			public ushort wParamL;
			public ushort wParamH;
		}

		[StructLayout( LayoutKind.Explicit )]
		struct INPUT
		{
			[FieldOffset( 0 )]
			public int type;
			[FieldOffset( 4 )] //*
			public MOUSEINPUT mi;
			[FieldOffset( 4 )] //*
			public KEYBDINPUT ki;
			[FieldOffset( 4 )] //*
			public HARDWAREINPUT hi;
		}

		[DllImport( "user32.dll" )]
		static extern IntPtr GetMessageExtraInfo();

		[DllImport( "user32.dll" )]
		static extern IntPtr GetFocus();

		[DllImport( "user32.dll" )]
		static extern IntPtr SetFocus( IntPtr hWnd );*/
	}
}